import { formatTimeDate } from '../../../../utils/formatData';
import Utils from "../../../../utils/utils";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    luckyBeansInfo: {}, // 幸运豆信息
    billList: [], // 幸运豆账单列表
    page: 1,
    pageSize: 10,
    totalNum: 0,
    dateText: formatTimeDate(new Date(), 'yyyy-MM'),
    dataTimeType: 'year-month',
    currentDate: new Date().getTime(),
    currentSelectDate: new Date().getTime(),
    selectTimeRadio: 'month',
    isShowSelectTime: false,
    formatter(type, value) {
      switch (type) {
        case 'year':
          return `${value}年`;
        case 'month':
          return `${value}月`;
        case 'day':
          return `${value}日`;
        default:
          return value;
      }
    }
  },
  initPage () {
    this.getData()
  },
  // 打开时间选择
  onOpenSelectTime () {
    this.setData({
      isShowSelectTime: true
    })
  },
  // 时间选择 - 取消
  onCancelTime () {
    this.setData({
      isShowSelectTime: false,
      currentDate: this.data.currentSelectDate
    })
  },
  // 时间选择 - 确定
  onConfirmTime () {
    const { selectTimeRadio, currentDate } = this.data;
    let dateText = '';
    if (selectTimeRadio === 'month') {
      dateText = formatTimeDate(new Date(currentDate), 'yyyy-MM')
    } else if (selectTimeRadio === 'day') {
      dateText = formatTimeDate(new Date(currentDate), 'yyyy-MM-dd')
    }
    this.setData({
      isShowSelectTime: false,
      currentSelectDate: currentDate,
      dateText: dateText,
      page: 1
    });
    this.getData()
  },

  // 修改时间类型选择单选框
  onChangeTimeRadio(event) {
    let dateType = '';
    let radioType = event.detail;
    if (radioType === 'month') {
      dateType = 'year-month'
    } else if (radioType === 'day') {
      dateType = 'date'
    }
    this.setData({
      selectTimeRadio: radioType,
      dataTimeType: dateType
    });
  },

  // 时间选择器 - 当值变化时触发的事假
  onInputPicker (event) {
    this.setData({
      currentDate: event.detail
    });
  },

  // 页面相关事件处理函数--监听用户下拉动作
  onPullDownRefresh: function () {
    this.setData({
      page: 1
    });
    this.getData()
  },

  // 页面上拉触底事件的处理函数
  onReachBottom: function () {
    this.setData({
      page: this.data.page + 1
    });
    this.getData()
  },
  // 获取提现记录
  getData () {
    const { dateText, selectTimeRadio, page, pageSize, billList } = this.data;
    let requestData = {};
    let self = this;
    if (selectTimeRadio === 'month') {
      requestData = {
        mouth: dateText,
        page: page,
        limit: pageSize
      }
    } else if (selectTimeRadio === 'day') {
      requestData = {
        day: dateText,
        page: page,
        limit: pageSize
      }
    }
    Utils.request({
      url:'/s/userinfo/PeaCashCardLog',
      data:requestData,
      loading:true,
      callback:function (result) {
        wx.stopPullDownRefresh();
        console.log(result);
        if(result.status === 'ok'){
          self.setData({
            totalNum: result.res.totalcount,
            billList:page === 1 ? result.res.list : billList.concat(result.res.list)
          });
        }
      }
    });
  },
});
